package com.hpf.community.utils;

import cn.hutool.core.lang.UUID;
import com.hpf.community.exception.CommonException;
import com.hpf.community.model.RoleAuthModel;
import com.hpf.community.model.UserModel;
import com.hpf.community.service.IRoleAuthService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description:
 * @Author: hpf
 * @Date: 2020/4/4 16:17
*/
public class CommonUtils {

    /**
     * 获取当前用户
     * @param: []
     * @return: com.hpf.community.pojo.User
     * @author: HPF
     * @date: 2020/4/4 16:23
     */
    public static UserModel getCurrentUser(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        UserModel user = (UserModel) request.getSession().getAttribute("loginUser");
        if(null != user){
            return user;
        }else {
            return null;
        }
    }

    /**
     * 获取UUID
     * @param: []
     * @return: java.lang.String
     * @author: HPF
     * @date: 2020/4/4 23:16
     */
    public static String getUUID(){
        return UUID.randomUUID(true).toString().replace("-","");
    }

    /**
     * 获取当前用户的姓名
     * @param: []
     * @return: java.lang.String
     * @author: HPF
     * @date: 2020/4/15 14:28
     */
    public static String getCurrentUserName(){
        return getCurrentUser().getName();
    }

    /**
     * 获取当前用户所在的社团id
     * @param: []
     * @return: java.lang.String
     * @author: HPF
     * @date: 2020/4/15 14:28
     */
    public static String getCurrentUserComId(){
        return getCurrentUser().getComId();
    }

    /**
     * 获取当前用户的所有权限
     * @param:
     * @return:
     * @author: HPF
     * @date: 2020/4/17 15:58
     */
    public static List<RoleAuthModel> getAuthListByCurrentRole(){
        //获取当前用户的role
        String role = getCurrentUser().getRole();
        if(StringUtils.isBlank(role)){
            throw new CommonException("登录过期，请重新登录");
        }
        //获取所有权限
        IRoleAuthService roleAuthService = (IRoleAuthService)SpringUtil.getBean(IRoleAuthService.class);
        List<RoleAuthModel> roleAuthModelList = roleAuthService.selectRoleAuthList(role);
        return  roleAuthModelList;
    }


    /**
     * 获取当前用户的姓名
     * @param: []
     * @return: java.lang.String
     * @author: HPF
     * @date: 2020/4/15 14:28
     */
    public static String getCurrentUserId(){
        return getCurrentUser().getId();
    }




}